package com.example.testdemo.resources;

import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;

import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;

import com.example.testdemo.TankGameActivity;

public class CharacterResources extends BaseResources {

	private final String TAG = CharacterResources.class.getSimpleName();

	public BuildableBitmapTextureAtlas mTextureEnemyAtlas;
	public BuildableBitmapTextureAtlas mPlayerTextureAtlas;
	public BuildableBitmapTextureAtlas mTextureAtlasController;

	public ITextureRegion mLifeTextureRegion;
	public ITextureRegion mFcControllerBaseTextureRegion;
	public ITextureRegion mFcControllerKnobTextureRegion;

	public ITiledTextureRegion mButtonTextureRegion;
	public ITiledTextureRegion mTankPlayer1TextureRegion;
	public ITiledTextureRegion mTankEnemy1TextureRegion;
	public ITiledTextureRegion mShieldTextureRegion;
	public ITiledTextureRegion mBulletTextureRegion;

	public Sound mSoundShoot;
	public Sound mSoundBomb;
	public Sound mSoundShield;
	public Font mFont;

	public CharacterResources(TankGameActivity activity) {
		super(activity);
	}
	
	@Override
	public void load(int resources) {
	    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("characters/");
        mTextureEnemyAtlas = new BuildableBitmapTextureAtlas(
                mActivity.getTextureManager(), 512, 512, TextureOptions.BILINEAR);
        mPlayerTextureAtlas = new BuildableBitmapTextureAtlas(
                mActivity.getTextureManager(), 512, 512, TextureOptions.BILINEAR);
        mTextureAtlasController = new BuildableBitmapTextureAtlas(
                mActivity.getTextureManager(), 512, 512, TextureOptions.BILINEAR);

        // characters
        mTankPlayer1TextureRegion = BitmapTextureAtlasTextureRegionFactory
                .createTiledFromAsset(mPlayerTextureAtlas, mActivity,
                        "tank_player_1_tiled.png", 5, 2);
        mShieldTextureRegion = BitmapTextureAtlasTextureRegionFactory
                .createTiledFromAsset(mPlayerTextureAtlas, mActivity,
                        "shield_tiled.png", 3, 1);
        mTankEnemy1TextureRegion = BitmapTextureAtlasTextureRegionFactory
                .createTiledFromAsset(mTextureEnemyAtlas, mActivity,
                        "tank_enemy_1_tiled.png", 4, 2);
        mBulletTextureRegion = BitmapTextureAtlasTextureRegionFactory
                .createTiledFromAsset(mTextureEnemyAtlas, mActivity,
                        "bullet.png", 1, 1);

        // controller
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("controllers/");
        mLifeTextureRegion = BitmapTextureAtlasTextureRegionFactory
                .createFromAsset(mTextureAtlasController, mActivity, "life.png");
        mFcControllerBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory
                .createFromAsset(mTextureAtlasController, mActivity,
                        "onscreen_control_base_scaled.png");
        mFcControllerKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory
                .createFromAsset(mTextureAtlasController, mActivity,
                        "onscreen_control_knob_scaled.png");
        mButtonTextureRegion = BitmapTextureAtlasTextureRegionFactory
                .createTiledFromAsset(mTextureAtlasController, mActivity,
                        "button_tiled_scaled.png", 2, 1);
        try {
            mTextureEnemyAtlas
                    .build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
                            10, 10, 10));
            mPlayerTextureAtlas
                    .build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
                            10, 10, 10));
            mTextureAtlasController
                    .build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
                            10, 10, 10));
            mTextureEnemyAtlas.load();
            mPlayerTextureAtlas.load();
            mTextureAtlasController.load();
        } catch (TextureAtlasBuilderException e) {
            Log.e(TAG, "Create DemoResources failed !", e);
        }

        // font
        mFont = FontFactory.create(mActivity.getFontManager(),
                mActivity.getTextureManager(), 256, 256,
                Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32,
                Color.WHITE);
        mFont.load();

        try {
            mSoundShoot = SoundFactory.createSoundFromAsset(mActivity
                    .getEngine().getSoundManager(), mActivity,
                    "sounds/shoot.3gpp");
            mSoundBomb = SoundFactory.createSoundFromAsset(mActivity.getEngine()
                    .getSoundManager(), mActivity, "sounds/bomb.mp3");
            mSoundShield = SoundFactory.createSoundFromAsset(mActivity.getEngine()
                    .getSoundManager(), mActivity, "sounds/shield.3gpp");
        } catch (Exception e) {
            Log.e(TAG, "load music and sound failed !", e);
        }
	}

	@Override
	public void unload(int resources) {
		mTextureEnemyAtlas.unload();
		mFont.unload();
		mSoundShoot.release();
		mSoundBomb.release();
	}

}
